﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using WinnerCMS.Common;
using System.Linq;
using WinnerCMS.Data;
using WinnerCMS.Model;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Word。
    /// </summary>
    public class WordsDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(Words model)
        {

            using (Entities db = new Entities())
            {
                int num = db.Words.Where(x => x.Find == model.Find && x.Ban == model.Ban).Count();
                if (num <= 0)
                {
                    db.Words.Add(model);
                    db.SaveChangesAsync().Wait();
                }

            }

        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(Words model)
        {

            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;

                db.SaveChangesAsync().Wait();
            }
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("update CMS_Word set ");
            //strSql.Append("Find = @Find,");
            //strSql.Append("Replacement = @Replacement,");
            //strSql.Append("Ban = @Ban,");
            //strSql.Append("AdminName = @AdminName,");
            //strSql.Append("UpDateTime = @UpDateTime");
            //strSql.Append(" where Id = @Id");

            //SqlParameter[] arrParameter = new SqlParameter[6];
            //arrParameter[0] = new SqlParameter("Find", SqlDbType.NVarChar, 255);
            //arrParameter[1] = new SqlParameter("Replacement", SqlDbType.NVarChar, 255);
            //arrParameter[2] = new SqlParameter("Ban", SqlDbType.Int, 4);
            //arrParameter[3] = new SqlParameter("AdminName", SqlDbType.NVarChar, 50);
            //arrParameter[4] = new SqlParameter("UpDateTime", SqlDbType.DateTime);
            //arrParameter[5] = new SqlParameter("Id", SqlDbType.Int, 4);

            //arrParameter[0].Value = model.Find;
            //arrParameter[1].Value = model.Replacement;
            //arrParameter[2].Value = model.Ban;
            //arrParameter[3].Value = model.AdminName;
            //arrParameter[4].Value = DateTime.Now;
            //arrParameter[5].Value = model.Id;



            //{
            //    SQLBaseV2.ExecuteNonQuery(strSql.ToString(), arrParameter);
            //}
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Words where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.Words.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 取得要过滤的词汇
        /// <summary>
        /// 取得要过滤的词汇
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetFilterWords()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            using (Entities db = new Entities())
            {
                var models = from x in db.Words
                             where x.Ban == 0
                             orderby x.Id descending
                             select new
                             {
                                 find = x.Find,
                                 replacement = x.Replacement
                             };

                foreach (var v in models)
                {
                    if (!dic.Keys.Contains(v.find))
                    {
                        dic.Add(v.find, v.replacement);
                    }
                }

                return dic;
            }


        }

        #endregion

        #region 取得要禁止的广告词汇
        /// <summary>
        /// 取得要禁止的广告词汇
        /// </summary>
        /// <returns></returns>
        public IList<string> GetBanWords()
        {
            List<string> list = new List<string>();
            using (Entities db = new Entities())
            {
                var model = from x in db.Words
                            where x.Ban == 1
                            group x by x.Find;

                foreach (var v in model)
                {
                    foreach (Words info in v)
                    {
                        list.Add(info.Find);
                    }
                }
            }

            return list;
        }

        #endregion

        #region 取得该IP10分钟内的数据量
        /// <summary>
        /// 取得该IP10分钟内的数据量
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public int GetInfoNumIn(string IP, string TableName)
        {
            using (Entities db = new Entities())
            {
                DateTime dt = DateTime.Now.AddMinutes(10);
                int num = 0;
                if (TableName == "CMS_Comments")
                {
                    num = (from x in db.Comments
                           where x.IP == IP && (x.UpdateTime >= DateTime.Now && x.UpdateTime <= dt)
                           select x).Count();
                }
                else
                {
                    num = (from x in db.GuestBooks
                           where x.IP == IP && (x.UpdateTime >= DateTime.Now && x.UpdateTime <= dt)
                           select x).Count();
                }

                return num;
            }
        }

        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="ban"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Words> BindList(int ban, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from A in db.Words select A;
                if (ban == 0)
                {
                    query = query.Where(x => x.Ban == 0);
                }

                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }

        #endregion
    }
}